<script setup>
import { artDelChannelService, artGetChannelService } from '@/api/article'
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue'

const channelList = ref([])
const isload = ref()
const getChannelList = async () => {
  isload.value = true
  const res = await artGetChannelService()
  channelList.value = res.data.data
  isload.value = false
  // console.log(channelList.value)
}

getChannelList()

const handleEdit = (row, $index) => {
  dialog.value.open(row)
  // console.log(typeof row)
  console.log(row, $index)
}

const handleDelete = async (row) => {
  //异步请求 添加弹框
  await ElMessageBox.confirm('你确认要删除此分类信息吗', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  //发送删除请求
  await artDelChannelService(row.id)
  //弹出消息
  ElMessage.success('删除成功')
  //重新渲染页面
  getChannelList()
}

const dialog = ref()

const onAddChannel = () => {
  dialog.value.open({})
}

const onSuccess = () => {
  getChannelList()
}
</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel"> 添加分类 </el-button>
    </template>

    <!-- prop绑定数据 label绑定表头名称 -->
    <el-table :data="channelList" style="width: 100%" v-loading="isload">
      <el-table-column label="序号" width="100" type="index" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column label="操作" width="100">
        <!-- 这里的row就是channelList中的每一项 -->
        <template #default="{ row, $index }">
          <el-button
            plain
            :icon="Edit"
            circle
            type="primary"
            @click="handleEdit(row, $index)"
          ></el-button>
          <el-button
            plain
            :icon="Delete"
            circle
            type="danger"
            @click="handleDelete(row, $index)"
          >
          </el-button>
        </template>
      </el-table-column>
      <!-- 数据为空时显示 -->
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <ChannelEdit ref="dialog" @success="onSuccess"></ChannelEdit>
  </page-container>
</template>
<style lang="scss" scoped></style>
